package com.openlogic.common.core.utils;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.lang.management.ManagementFactory;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
* @ClassName: DateUtils
* @Description: 时间工具类
* @author weiyongpeng
* @date 2025年1月13日 上午9:44:08
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {

    public static final String YYYY = "yyyy";

    public static final String YYYY_MM = "yyyy-MM";

    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    
    public static final String YYYYMMDD = "yyyyMMdd";

    public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    private static final String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
    
    
    /**
	 * 要用到的DATE Format的定义
	 */
	public static String DATE_FORMAT_DATEONLY = "yyyy-MM-dd"; // 年/月/日
	public static String DATE_FORMAT_DATETIME = "yyyy-MM-dd HH:mm:ss"; // 年/月/日/时/分/秒

    /**
     * 获取当前Date型日期
     *
     * @return Date() 当前日期
     */
    public static Date getNowDate() {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDate() {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static String getTime() {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static String dateTimeNow() {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static String dateTimeNow(final String format) {
        return parseDateToStr(format, new Date());
    }

    public static String dateTime(final Date date) {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static String parseDateToStr(final String format, final Date date) {
        return new SimpleDateFormat(format).format(date);
    }

    public static Date dateTime(final String format, final String ts) {
        try {
            return new SimpleDateFormat(format).parse(ts);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static String datePath() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static String dateTime() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }
    
    public static Date parseDate() {
    	return new Date();
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str.toString(), parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }
    
    public static Date parseDate2(String date) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");

		if ((date == null) || (date.equals(""))) {
			return null;
		} else {
			try {
				return formatter.parse(date);
			} catch (ParseException e) {
				return null;
			}
		}
	}
    
    /**
     * 根据输入的日期，返回当天最后一刻
     * @param date
     * @return
     */
    public static Date parseDateEnd(String date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if ((date == null) || (date.equals(""))) {
            return null;
        } else {
            try {
                return formatter.parse(date+" 23:59:59");
            } catch (ParseException e) {
                return null;
            }
        }
    }

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }


    /**
     * 增加 LocalDateTime ==> Date
     */
    public static Date toDate(LocalDateTime temporalAccessor) {
        ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
        return Date.from(zdt.toInstant());
    }

    /**
     * 增加 LocalDate ==> Date
     */
    public static Date toDate(LocalDate temporalAccessor) {
        LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
        ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
        return Date.from(zdt.toInstant());
    }

	public static Timestamp getCurTimestamp() {
		
		return new Timestamp(nowTimeMillis());
	}
	
	/**
	 * 获得当前的日期毫秒
	 * 
	 * @return
	 */
	public static long nowTimeMillis() {
		return System.currentTimeMillis();
	}
	
	/**
	 * 根据指定的Format转化java.util.Date到String
	 * 
	 * @param dt
	 *            java.util.Date instance
	 * @param sFmt
	 *            Date format , DATE_FORMAT_DATEONLY or DATE_FORMAT_DATETIME
	 * @return
	 * @since 1.0
	 * @history
	 */
	public static String toString(java.util.Date dt, String sFmt) {
		if (dt == null || sFmt == null || "".equals(sFmt)) {
			return "";
		}
		return toString(dt, new SimpleDateFormat(sFmt));
	}

	/**
	 * 利用指定SimpleDateFormat instance转换java.util.Date到String
	 * 
	 * @param dt
	 *            java.util.Date instance
	 * @param formatter
	 *            SimpleDateFormat Instance
	 * @return
	 * @since 1.0
	 * @history
	 */
	private static String toString(java.util.Date dt, SimpleDateFormat formatter) {
		String sRet = null;

		try {
			sRet = formatter.format(dt).toString();
		} catch (Exception e) {
			sRet = null;
		}

		return sRet;
	}

	/**
	 * 转换java.sql.Timestamp到String，格式为YYYY/MM/DD HH24:MI
	 * 
	 * @param dt
	 *            java.sql.Timestamp instance
	 * @return
	 * @since 1.0
	 * @history
	 */
	public static String toSqlTimestampString2(java.sql.Timestamp dt) {
		if (dt == null) {
			return null;
		}
		String temp = toSqlTimestampString(dt, DateUtils.DATE_FORMAT_DATETIME);
		return temp.substring(0, 16);
	}

	public static String toString(java.sql.Timestamp dt) {
		return dt == null ? "" : toSqlTimestampString2(dt);
	}

	/**
	 * 根据指定的格式转换java.sql.Timestamp到String
	 * 
	 * @param dt
	 *            java.sql.Timestamp instance
	 * @param sFmt
	 *            Date 格式，DATE_FORMAT_DATEONLY/DATE_FORMAT_DATETIME/
	 *            DATE_FORMAT_SESSION
	 * @return
	 * @since 1.0
	 * @history
	 */
	public static String toSqlTimestampString(java.sql.Timestamp dt, String sFmt) {
		String temp = null;
		String out = null;
		if (dt == null || sFmt == null) {
			return null;
		}
		temp = dt.toString();
		if (sFmt.equals(DateUtils.DATE_FORMAT_DATETIME) || // "YYYY/MM/DD
				// HH24:MI:SS"
				sFmt.equals(DateUtils.DATE_FORMAT_DATEONLY)) { // YYYY/MM/DD
			temp = temp.substring(0, sFmt.length());
			out = temp.replace('/', '-');
			// }else if( sFmt.equals (DateUtils.DATE_FORMAT_SESSION ) ){
			// //Session
			// out =
			// temp.substring(0,4)+temp.substring(5,7)+temp.substring(8,10);
			// out += temp.substring(12,14) + temp.substring(15,17);
		}
		return out;
	}
	
	/**
	 * 传入时间字符串及时间格式，返回对应的Date对象
	 * 
	 * @param src
	 *            时间字符串
	 * @param pattern
	 *            时间格式
	 * @return Date
	 */
	public static java.util.Date getDateFromString(String src, String pattern) {
		SimpleDateFormat f = new SimpleDateFormat(pattern);
		try {
			return f.parse(src);
		} catch (ParseException e) {
			return null;
		}
	}
	
	public static Date getDate(Object o) {
		if (o == null) {
			return null;
		} else if (o instanceof Date) {
			return (Date) o;
		} else if (o instanceof String) {
			return getDate(String.valueOf(o));
		} else if (o instanceof java.sql.Timestamp) {
			return new Date(((java.sql.Timestamp) o).getTime());
		} else if (o instanceof java.sql.Date) {
			return new Date(((java.sql.Date) o).getTime());
		} else {
			return null;
		}
	}
    
	/**
	 * 取得通用日期时间格式字符串
	 * 
	 * @param Date
	 * @return String
	 */
	public static String formatDate(Date date) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return dateFormat.format(date);
	}
	
	/**
	 * 取得指定日期格式的字符串
	 * 
	 * @param Date
	 * @return String
	 */
	public static String formatDate(Date date, String format) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		return dateFormat.format(date);
	}


	/**
	 * 获取当日的日期格式串
	 * 
	 * @param
	 * @return String
	 */
	public static String today() {
		return formatDate(new Date(), "yyyy-MM-dd");
	}

	/**
	 * 获取当前时间格式串
	 * 
	 * @param
	 * @return String
	 */
	public static String currentTime() {
		return formatDate(new Date(), "yyyyMMddhhmmssSSS");
	}
	
	/**
	 * 转化10位的日期字符串为8位的日期字符串
	 * 
	 * @param date
	 * @return
	 */
	public static String tranDate10To8(String date) {
		if (date != null && date.length() == 10) {
			return date.replaceAll("-", "");
		}
		return date;
	}

	/**
	 * 转化8位的日期字符串为10位的日期字符串
	 * 
	 * @param date
	 * @return
	 */
	public static String tranDate8To10(String date) {
		if (date != null && date.length() == 8) {
			return date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6);
		}
		return date;
	}
	
	/**
	 * 计算 day 天后的时间
	 * 
	 * @param date
	 * @param day
	 * @return
	 */
	public static Date addDay(Date date, int day) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, day);
		return calendar.getTime();
	}
	
	/**
     * 计算 day 天后的时间
     * 
     * @param dateStr      格式：yyyyMMdd
     * @param day   
     * @return  date    格式：yyyyMMdd
	 * @throws ParseException 
     */
    public static String addDay(String dateStr, int day) throws ParseException {
    	SimpleDateFormat SHORTDATEFORMAT = new SimpleDateFormat("yyyyMMdd");
        Date date = SHORTDATEFORMAT.parse(dateStr);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, day);
        return SHORTDATEFORMAT.format(calendar.getTime());
    }
    
    
	
	public static Date parseDate(String str, String parsePatterns) {
		try {
			return parseDate(str, new String[] { parsePatterns });
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new Date();
	}
	/**
	 * 判断当前时间是否在一定的时间范围内
	 * 
	 * @param String
	 * @return boolean
	 */
	public static boolean isInBetweenTimes(String startTime, String endTime) {
		Date nowTime = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		String time = sdf.format(nowTime);
		if (time.compareTo(startTime) >= 0 && time.compareTo(endTime) <= 0) {
			return true;
		} else {
			return false;
		}
	}	
	
	/**
	 * 得到当前年起始时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getYearStart(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
		calendar.set(Calendar.MONTH, 0);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}
	
	/**
	 * 字符串转date
	 * 
	 * @param dateString
	 * @return
	 */
	public static Date StringToDate(String dateString) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		Date date = null;
		try {
			date = sdf.parse(dateString);
		} catch (ParseException e) {
		}
		return date;
	}
	public static String dateToStrYYYMMDD(Date date) {
		return toString(date, "yyyyMMdd");
	}
	
	/**
	 * 获取当前日期位于当年的第几天
	 * 
	 * @param ldt
	 * @return
	 */
	public static int getDayOfYear(LocalDateTime ldt) {
		int dayOfYear = ldt.getDayOfYear();
		return dayOfYear;
	}
	
	/**
	 * 获取当前日期位于当月的第几天
	 * 
	 * @param ldt
	 * @return
	 */
	public static int getDayOfMonth(LocalDateTime ldt) {
		int dayOfMonth = ldt.getDayOfMonth();
		return dayOfMonth;
	}
	
	/**
	 * 获取当前日期位于当季的第几天
	 * 
	 * @param ldt
	 * @return
	 */
	@SuppressWarnings("unused")
	public static int getDayOfQuater(LocalDateTime ldt) {
		// 系统当前时间
		LocalDateTime now = LocalDateTime.now();
		// 系统当前日期
		LocalDate localDate = convertLocalDateTimeToLocalDate(ldt);
		// 获取当前时间位于当季的第一天
		LocalDate firstDayOfQuaterOfLocaldate = getFirstDayOfQuaterOfLocaldate(localDate);
		long endDay = localDate.toEpochDay();
		long stratDay = firstDayOfQuaterOfLocaldate.toEpochDay();
		int betweenDay = Integer.parseInt(String.valueOf(endDay-stratDay));
		return betweenDay;
	}
	
	/**
	 * 获取当前日期位于当周的第几天
	 * 
	 * @param ldt
	 * @return
	 */
	public static int getDayOfWeek(LocalDateTime ldt) {
		int dayOfWeek = ldt.getDayOfWeek().getValue();
		return dayOfWeek;
	}
	
	/**
	 * 获取当前时间在当年的第一天
	 * @return
	 */
	public static String getFirstDayOfYear(LocalDate ld) {
		LocalDate dayOfYear = ld.with(TemporalAdjusters.firstDayOfYear());
		DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");
		String dayOfYearStr = dtf.format(dayOfYear);
		return dayOfYearStr;
	}
	
	/**
	 * 获取当前时间在当季的第一天
	 * @return String
	 */
	public static String getFirstDayOfMonth(LocalDate ld) {
		LocalDate dayOfMonth = ld.with(TemporalAdjusters.firstDayOfMonth());
		DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");
		String dayOfMonthStr = dtf.format(dayOfMonth);
		return dayOfMonthStr;
	}
	
	/**
	* 描述：转化字符8位日期为LocalDate
	* @Title: convertDateStr8ToLocalDate
	* @param dateStr
	* @return
	* @author weiyongpeng
	* @date  2024年4月7日 下午3:17:47
	 */
	public static LocalDate convertDateStr8ToLocalDate(String dateStr) {
		Date date = parseDate(dateStr, DateUtils.YYYYMMDD);
		Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        // atZone()方法返回在指定时区从此Instant生成的ZonedDateTime。
        LocalDate localDate = instant.atZone(zoneId).toLocalDate();
        return localDate;
	}
	
	/**
	* 描述：获取上月末日期字符串 - 8位
	* @Title: getLastDayOfLastMonth
	* @param localDate
	* @return
	* @author weiyongpeng
	* @date  2024年4月7日 下午3:19:57
	 */
	public static String getLastDayOfLastMonth8(LocalDate localDate) {
		// 步骤1：设置日期为月初
        LocalDate firstDayOfMonth = localDate.withDayOfMonth(1);
        // 步骤2：减去一天
        LocalDate lastDayOfLastMonth = firstDayOfMonth.minusDays(1);
        // 步骤3：格式化日期
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String formattedDate = lastDayOfLastMonth.format(formatter);
        // 步骤4：返回格式化后的日期
        return formattedDate;
	}
	
	/**
	 * 获取当前时间在当月的第一天
	 * @return String
	 */
	public static String getFirstDayOfQuater(LocalDate ld) {
		LocalDate quaterOfLocaldate = getFirstDayOfQuaterOfLocaldate(ld);
		DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");
		String dayOfQuaterStr = dtf.format(quaterOfLocaldate);
		return dayOfQuaterStr;
	}
	
	/**
	 * 获取当前日期的前num天
	 * @param ld
	 * @param num 天数
	 * @return
	 */
	public static String getPreDateStr(LocalDate ld,int num) {
		LocalDate minusDays = ld.minusDays(num);
		DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");
		String preDayStr = dtf.format(minusDays);
		return preDayStr;
	}
	
	/**
	 * 获取当前日期的后num天
	 * @param ld
	 * @param num 天数
	 * @return
	 */
	public static String getAfterDateStr(LocalDate ld,int num) {
		LocalDate plusDays = ld.plusDays(num);
		DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");
		String plusDayStr = dtf.format(plusDays);
		return plusDayStr;
	}

	/**
	 * 转化当前LocalDateTime为LocalDate
	 *
	 * @param ldt
	 * @return
	 */
	public static LocalDate convertLocalDateTimeToLocalDate(LocalDateTime ldt) {
		// 将当前时间转化为date
		Date date = Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant());
		// 将date类型的日期转化为LocalDate
		LocalDate localDate = date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDate();
		return localDate;
	}
	
	/**
	 * 获取当前时间所在季度的第一天
	 * 
	 * @param ld
	 * @return Localdate
	 */
	@SuppressWarnings("static-access")
	public static LocalDate getFirstDayOfQuaterOfLocaldate(LocalDate ld) {
		int monthValue = ld.getMonthValue();
		LocalDate firstDayOfQuater;
		if(monthValue <= 3) {
			firstDayOfQuater = ld.of(ld.getYear(), Month.JANUARY, 1);
		}else if(monthValue <= 6) {
			firstDayOfQuater = ld.of(ld.getYear(), Month.APRIL, 1);
		}else if(monthValue <= 9) {
			firstDayOfQuater = ld.of(ld.getYear(), Month.JULY, 1);
		}else {
			firstDayOfQuater = ld.of(ld.getYear(), Month.OCTOBER, 1);
		}
		return firstDayOfQuater;
	}
	
	/**
	* 描述：将Long型的时间戳转化为日期字符串
	* @Title: timeToString
	* @param time
	* @param format
	* @return
	* @throws DateTimeParseException
	* @author weiyongpeng
	* @date  2023年8月22日 下午7:42:15
	 */
	@SuppressWarnings("null")
	public static String timeToString(Long time,String format) throws DateTimeParseException{
		try {
			if (time == null && time.equals("")) {
				System.out.println("时间不能为空！！！");
			}
			DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern(format);
			String timeformat = ofPattern.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()));
			return timeformat;
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}
	
	/**
	* 描述：计算一个日期类型的参数，正向偏移amount后的时间
	* @Title: offsetDate
	* @param dateFormString
	* @param field field the calendar field.
	* @param amount amount the amount of date or time to be added to the field.
	* @return
	* @author weiyongpeng
	* @date  2023年8月22日 下午7:44:27
	 */
	public static Date offsetDate(Date dateFormString,int field,int amount) {
		Calendar instance = Calendar.getInstance();
		instance.setTime(dateFormString);
		instance.add(field, amount);
		Date time = instance.getTime();
		return time;
	}
	
	/**
	* 描述：计算两个日期的间隔天数
	* @Title: betweenDate8Str
	* @param startDateStr
	* @param endDateStr
	* @return
	* @author weiyongpeng
	* @date  2024年4月6日 下午4:15:53
	 */
	public static int betweenDayDate8Str(String startDateStr, String endDateStr) {
		DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern(DateUtils.YYYYMMDD);
		LocalDate startDate = LocalDate.parse(startDateStr,ofPattern);
		LocalDate endDate = LocalDate.parse(endDateStr,ofPattern);
		long day = ChronoUnit.DAYS.between(startDate, endDate);
		return Integer.valueOf(day+"");
	}
	
	/**
	 * 
	* 描述：日期格式
	* @Title: formatDate
	* @param date
	* @return
	* @author yangfan
	* @date  2024年5月9日 下午4:50:48
	 */
	public static String format(Date date) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
		return dateFormat.format(date);
	}
	

    /**
     * 计算两个日期之间的天数差（以毫秒为单位）
     *
     * @param date1 第一个日期
     * @param date2 第二个日期
     * @return 两个日期之间的天数差的绝对值
     */
    public static int differentDaysByMillisecond(Date date1, Date date2) {
        return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24)));
    }

	/**
     * 计算两个时间点的差值（天、小时、分钟、秒），当值为0时不显示该单位
     *
     * @param endDate 结束时间
     * @param nowDate 当前时间
     * @return 时间差字符串，格式为 "x天 x小时 x分钟 x秒"，若为 0 则不显示
     */
    public static String getTimeDifference(Date endDate, Date nowDate) {
        long diffInMillis = endDate.getTime() - nowDate.getTime();
        long day = TimeUnit.MILLISECONDS.toDays(diffInMillis);
        long hour = TimeUnit.MILLISECONDS.toHours(diffInMillis) % 24;
        long min = TimeUnit.MILLISECONDS.toMinutes(diffInMillis) % 60;
        long sec = TimeUnit.MILLISECONDS.toSeconds(diffInMillis) % 60;
        // 构建时间差字符串，条件是值不为0才显示
        StringBuilder result = new StringBuilder();
        if (day > 0) {
            result.append(String.format("%d天 ", day));
        }
        if (hour > 0) {
            result.append(String.format("%d小时 ", hour));
        }
        if (min > 0) {
            result.append(String.format("%d分钟 ", min));
        }
        if (sec > 0) {
            result.append(String.format("%d秒", sec));
        }
        return result.length() > 0 ? result.toString().trim() : "0秒";
    }
	
}
